/**
 * @author KujoMiva
 * @name uploadHandler.js
 * @desc 获取阿里云存储临时密钥
 * @time 2020年05月26日 18:09:45 星期二
 */
import store from "@/store";
import OSS from "ali-oss";

/** @type {OSS} Client */
let Client = null;

export default async function generateOSS() {
    // 获取临时密钥
    const result = await store.dispatch("sts/stsDispatcherAction");

    // 返回 true过来就说明获取临时密钥成功, 这里就创建一个实例. 否则使用旧实例
    if (result === true || Client === null) {
        // 获取临时密钥
        const ossOptions = store.state.sts.stsCache;

        // 创建实例
        Client = new OSS({
            bucket: "km-oss-resource",
            endpoint: "km-oss-resource.oss-cn-hangzhou.aliyuncs.com",
            stsToken: ossOptions.SecurityToken,
            accessKeyId: ossOptions.AccessKeyId,
            accessKeySecret: ossOptions.AccessKeySecret,
            cname: true,
            secure: true,
        });

        return Client;
    }

    return Client;
}
